package com.px.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.px.client.CommodityFeignClient;
import com.px.client.OrderFeignClient;
import com.px.common.Constants;
import com.px.dto.ReturnResult;
import com.px.dto.ReturnResultUtils;
import com.px.pojo.ShCommodity;
import com.px.pojo.ShOrder;
import com.px.pojo.ShUser;
import com.px.utils.EmptyUtils;
import com.px.utils.RedisUtil;
import com.px.vo.OrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderFeignClient orderFeignClient;

    @Resource
    private CommodityFeignClient commodityFeignClient;

    @Resource
    private ObjectMapper objectMapper;

    @Resource
    private RedisUtil redisUtil;

    //根据用户ID获取订单信息
    @Override
    public ReturnResult selectOrderByUserId(String token, Model model) throws Exception {
        String userJson = redisUtil.getStr(token);
        if (EmptyUtils.isEmpty(userJson)){
            return ReturnResultUtils.returnFail(10001,"登录超时，请重新登录");
        }
        ShUser shUser = objectMapper.readValue(userJson, ShUser.class);
        Integer userId = shUser.getUserId();
        List<ShOrder> shOrderList = orderFeignClient.selectOrderByUserId(userId);
        List<OrderVo> orderVoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(shOrderList)){
            for (ShOrder shOrder:shOrderList){
                OrderVo orderVo = new OrderVo();
                BeanUtils.copyProperties(shOrder,orderVo);
                //获取图片
                String key = Constants.goodsPrefix + shOrder.getCommodityId();
                String goodsJson = redisUtil.getStr(key);
                //判断redis中有没有图片
                if (EmptyUtils.isNotEmpty(goodsJson)){
                    OrderVo vo = objectMapper.readValue(goodsJson, OrderVo.class);
                    orderVo.setCommodityCover(vo.getCommodityCover());
                }else {
                    //根据商品ID查询商品信息
                    ShCommodity shCommodity = commodityFeignClient.selectGoodsById(shOrder.getCommodityId());
                    orderVo.setCommodityCover(shCommodity.getCommodityCover());
                }
                orderVoList.add(orderVo);
            }
            model.addAttribute("orderList",orderVoList);
        }

        return ReturnResultUtils.returnSuccess(orderVoList);
    }

    @Override
    public ReturnResult selectOrderByOrderId(Integer orderId,String token) throws Exception {
        String userJson = redisUtil.getStr(token);
        if (EmptyUtils.isEmpty(userJson)){
            return ReturnResultUtils.returnFail(10001,"登录超时，请重新登录");
        }
        ShOrder shOrder = orderFeignClient.selectOrderByOrderId(orderId);
        return ReturnResultUtils.returnSuccess(shOrder);
    }
}
